﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace printdesign
{
    /// <summary>
    /// AddedTextBlock.xaml 的交互逻辑
    /// </summary>
    public partial class AddedTextBlock : UserControl
    {
        TextBlock tb = null;
        public AddedTextBlock()
        {
            InitializeComponent();
            tb=this.FindName()
            tb.MouseLeftButtonDown += Tb_MouseLeftButtonDown;
            tb.MouseMove += Tb_MouseMove;
            tb.MouseRightButtonUp += Tb_MouseRightButtonUp;           
        }
        Control parent = null;
        public AddedTextBlock(Control _parent)
        {
            InitializeComponent();
            tb.MouseLeftButtonDown += Tb_MouseLeftButtonDown;
            tb.MouseMove += Tb_MouseMove;
            tb.MouseRightButtonUp += Tb_MouseRightButtonUp;
            parent = _parent;
        }
        private void Tb_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
        {
            TextBlock tmp = (TextBlock)sender;
            tmp.ReleaseMouseCapture();
        }

        private void Tb_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                TextBlock tmp = (TextBlock)sender;
                double dx = e.GetPosition(null).X - pos.X + tmp.Margin.Left;
                double dy = e.GetPosition(null).Y - pos.Y + tmp.Margin.Top;
                tmp.Margin = new Thickness(dx, dy, 0, 0);
                pos = e.GetPosition(parent);
            }
        }
        Point pos = new Point();
        private void Tb_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            TextBlock tmp = (TextBlock)sender;
            pos = e.GetPosition(parent);
            tmp.CaptureMouse();
            tmp.Cursor = Cursors.Hand;
        }

        private void UserControl_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            ChooseField filed = new ChooseField(typeof(Student));
            tb.ReleaseMouseCapture();
            filed.Closed +=(a,b)=> {
                if (!string.IsNullOrEmpty(filed.Textsave))
                {
                    tb.Text = "[" + filed.Textsave + "]";
                }
            };
            filed.Show();
        }
    }
}
